<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <style>
    </style>
</head>
<style>
    .home:before {
        content: url(../image/home.png);
    }

    .refresh:before {
        content: url(../image/refresh.png);
    }

    .company:before {
        content: url(../image/company.png);
    }

    .pa-title {
        font-size: 17px;
        color: black;
        font-weight: bold;
    }

    .pa-title-content {
        font-size: 17px;
        color: black;
    }

    body {
        background-color: #D9D8DB;
    }

    .pa_mould_t {
        background-color: #254060;
        height: 443px;
        color: white;
        overflow: auto;
    }

    .pa_mould_but {
        height: 58px;
        line-height: 58px;
        float: right;
        width: 150px;
        font-size: 30px;
        color: white;
        text-align: center;
        margin-right: 40px;
    }

    p {
        font-size: 17px;
        color: black;
        font-weight: bold;
        text-align: center;
    }

    table {
        width: 100%;
        table-layout: fixed;
        word-wrap: break-word;
    }

    td {
        white-space: nowrap;
        text-overflow: ellipsis;
        overflow: hidden;
    }
</style>

<body>
    <header class="aui-bar aui-bar-nav" style="background-color: #D9D8DB">
        <div style="width: 10%;height: 60px;float: left;padding-top: 6px;">
            <a class="aui-pull-left aui-btn ">
                <span class="aui-iconfont company"></span>
            </a>
        </div>
        <div style="width: 10%;height: 60px;float: left;">
            <div style="text-align: left;">
                <span class="pa-title">富晟安创</span>
            </div>
        </div>
        <div style="width: 70%;height: 60px;float: left;">
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="locationName"></span>
            </div>
            <div style="width:18%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCenterName"></span>
            </div>
            <div style="width:24%; float: left; text-align: left;">
                <span class="pa-title-content" id="workCellName"></span>
            </div>
            <div style="width:40%; float: left; text-align: left;">
                <span class="pa-title-content" id="nowDate"></span>
            </div>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn " onclick="refresh()">
                <span class="aui-iconfont refresh"></span>
            </a>
        </div>
        <div style="width: 5%;height: 50px;float: left;">
            <a class="aui-pull-right aui-btn" onclick="closeWin()">
                <span class="aui-iconfont home"></span>
            </a>
        </div>
    </header>
    <div class="aui-content">
        <div class="aui-flex-col aui-flex-center" style="height: 30px;">
            <div class="aui-flex-item-12">
                <p>模具更换</p>
            </div>
        </div>
    </div>

    <div class="aui-flex-col aui-flex-center">
        <div class="aui-flex-item-12 pa_mould_t">
            <div class="aui-flex-col" style="height: 150px;">
                <div class="aui-flex-item-12">
                    <div style="height: 100%;width: 100%;overflow: auto;border-bottom: 1px solid gray;">
                        <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                            <thead>
                                <tr style="text-align: center;color: white;font-size: 15px;height: 35px;">
                                    <td algin='center' style="width:3%"></td>
                                    <td algin='center' style="width:5%">序号</td>
                                    <td algin='center' style="width:16%">工单号</td>
                                    <td algin='center' style="width:17%">物料编码</td>
                                    <td algin='center' style="width:16%">物料名称</td>
                                    <td algin='center' style="width:8%">计划数</td>
                                    <td algin='center' style="width:5%">状态</td>
                                    <td algin='center' style="width:15%">计划换模时间</td>
                                    <td algin='center' style="width:15%">实际换模时间</td>
                                </tr>
                            </thead>
                            <tbody id="workOrderHtml">
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
            <div class="aui-flex-col" style="height: 80px;">
                <div class="aui-flex-item-12" style="padding-top:10px;">
                    <div class="aui-list-item-label"
                        style="font-size: 20px;line-height: 60px;margin-left: 10px;float: left;">扫码绑定：
                    </div>
                    <div class="aui-list-item-input" style="background-color: white; float: left;width: 400px;">
                        <input type="text" class="aui-input" id="rsBarCode" onclick="getIdSelector();"
                            placeholder="模具条码">
                    </div>
                </div>
            </div>
            <div class="aui-flex-col" style="height: 210px;overflow: auto;">
                <div class="aui-flex-item-12">
                    <table class="table" border="1px solid gray" cellspacing="0" cellpadding="0">
                        <thead>
                            <tr style="text-align: center;color: white;font-size: 15px;height: 35px;">
                                <td algin='center' style="width:5%">序号</td>
                                <td algin='center' style="width:15%">模具编码</td>
                                <td algin='center' style="width:15%">模具名称</td>
                                <td algin='center' style="width:30%">模具条码</td>
                                <td algin='center' style="width:25%">绑定时间</td>
                                <td algin='center' style="width:10%">操作</td>
                            </tr>
                        </thead>
                        <tbody id="mouldHtml">
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
<script type="text/javascript" src="../script/public.js"></script>
<script type="text/javascript">

    var equipUniqueId = "";
    //光标所在id
    var idSelector = "";

    //初始化
    apiready = function () {
        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');
        //产线工位信息
        document.getElementById("locationName").innerHTML = api.pageParam.locationName;
        document.getElementById("workCenterName").innerHTML = api.pageParam.workCenterName;
        document.getElementById("workCellName").innerHTML = api.pageParam.workCellName;
        document.getElementById("nowDate").innerHTML = getTimeCommon();
        //当前时间刷新
        window.setInterval("getTime()", 1000);

        //1：查询换模计划
        doSearch();

        api.setFocus({
            inputId: 'rsBarCode',
            focus: true
        });
    }

    // 获取光标所在组件的id
    function getIdSelector() {
        var e = event || window.event;
        var el = e.srcElement || e.target;
        idSelector = el.id;
        $("#" + el.id).select();
    }

    //时间刷新
    function getTime() {
        document.getElementById("nowDate").innerHTML = getTimeCommon();
    }

    //查询发布和暂停的工单信息
    function doSearch() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyWorkOrder";
        var data = {
            values: {
                proddate: api.pageParam.proddate,
                workCenterCode: api.pageParam.workCenterCode,
                shiftCode: api.pageParam.shiftCode,
                state: "发布,暂停",
                type: "换模计划",
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: white;font-size: 13px;height:35px;'>";
                        html += "<td><input name='sub' type='checkbox' /></td>";
                        html += "<td name='index'>" + (i + 1) + "</td>";
                        html += "<td name='workOrderCode'>" + list[i].workOrderCode + "</td>";
                        html += "<td name='mrlCode'>" + list[i].mrlCode + "</td>";
                        html += "<td name='mrlName'>" + list[i].mrlName + "</td>";
                        html += "<td name='planQty'>" + list[i].planQty + "</td>";
                        html += "<td name='status'>" + list[i].state + "</td>";
                        html += "<td name='planMouldDate'>" + list[i].planMouldDate + "</td>";
                        html += "<td name='actualMouldDate'>" + list[i].actualMouldDate + "</td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("workOrderHtml"), html);

                    if (list != null && list.length > 0) {
                        //查询当前模具
                        doSearchMould();
                    }else{
                        $api.html($api.byId("mouldHtml"), "");
                    }
                } else {
                    $api.html($api.byId("workOrderHtml"), "");
                    $api.html($api.byId("mouldHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //查询当前产线所有模具
    function doSearchMould() {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyMouldWorkOrder";
        var data = {
            values: {
                workCenterCode: api.pageParam.workCenterCode,
                un: un,
            }
        };
        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                if (myobj.code == 0) {
                    var list = eval(myobj.list);
                    var html = "";
                    //遍历
                    for (var i = 0; i < list.length; i++) {
                        html += "<tr style='text-align: center;color: white;font-size: 13px;height:35px;'>";
                        html += "<td name='index'>" + (i + 1) + "</td>";
                        html += "<td name='rsCode'>" + list[i].rsCode + "</td>";
                        html += "<td name='rsName'>" + list[i].rsName + "</td>";
                        html += "<td name='rsBarCode'>" + list[i].rsBarCode + "</td>";
                        html += "<td name='bindDate'>" + list[i].bindDate + "</td>";
                        html += "<td name='operation'> <button style = 'background-color:red;' onclick='unBindSave(\"" + list[i].rsBarCode + "\")'>解绑</button></td>";
                        html += "</tr>";
                    }

                    $api.html($api.byId("mouldHtml"), html);
                } else {
                    $api.html($api.byId("mouldHtml"), "");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                }
            } else {
                alert("网络连接超时!")
            }
        });
    }

    //绑定保存
    function bindSave() {
        //机台
        var rsBarCode = $("#rsBarCode").val();
        if (rsBarCode == null || rsBarCode == "" || rsBarCode == undefined) {
            api.toast({
                msg: "请扫描模具条码!",
                duration: 3000
            });
            return false;
        }

        //选中数据
        var check = $("input[type='checkbox']:checked");
        var count = 0;
        var workOrderCode = "";
        check.each(function () { //遍历
            var row = $(this).parent("td").parent("tr");
            workOrderCode = row.find("[name='workOrderCode']").html();
            count++;

        });

        if (count != 1) {
            api.toast({
                msg: "请选择一条工单数据!",
                duration: 3000
            });
            return;
        }

        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyMouldBindSave";
        var data = {
            values: {
                rsBarCode: rsBarCode,
                workOrderCode: workOrderCode,
                workCenterCode: api.pageParam.workCenterCode,
                un: un
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //刷新表数据
                    refresh();

                } else {
                    $("#rsBarCode").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    setFocus();
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    //解绑
    function unBindSave(rsBarCode) {
        var un = localStorage.getItem('un');
        var serverUrl = localStorage.getItem('serverUrl');
        var url = serverUrl + "/app/getAssemblyMouldUnBindSave";
        var data = {
            values: {
                rsBarCode: rsBarCode,
                workCenterCode: api.pageParam.workCenterCode,
                un: un
            }
        };

        var params = new Object();
        params.url = url;
        params.dataType = 'json';
        params.method = 'post';

        api.showProgress({
            title: '保存中...',
        });

        ajaxForData(params, data, false, function (ret, err) {
            if (ret) {
                api.hideProgress();
                var data = JSON.stringify(ret);
                var myobj = eval("(" + data + ")");
                //成功
                if (myobj.code == 0) {
                    api.toast({
                        msg: "保存成功!",
                        duration: 3000
                    });

                    //刷新表数据
                    refresh();

                } else {
                    $("#rsBarCode").val("");
                    api.toast({
                        msg: myobj.message,
                        duration: 3000
                    });
                    setFocus();
                }
            } else {
                api.hideProgress();
                alert("网络连接超时!")
            }
        });
    }

    function setFocus() {
        api.setFocus({
            inputId: 'rsBarCode',
            focus: true
        });
    }

    //模具条码扫描绑定
    $("#rsBarCode").on('keypress', function (e) {
        var keycode = e.keyCode;
        var tm = $(this).val();
        if (keycode == '13') {
            if (tm != null && tm != "" && tm != undefined) {
                $("#rsBarCode").blur();

                //绑定保存
                bindSave();
            }
        }
    });

    //刷新按钮
    function refresh() {

        //获取设备唯一号
        equipUniqueId = localStorage.getItem('equipUniqueId');

        $("#rsBarCode").val("");
        //查询发布和暂停的工单换模计划信息
        doSearch();

        api.setFocus({
            inputId: 'rsBarCode',
            focus: true
        });
    }

    //取消
    function cancal() {
        api.closeWin({});
        api.execScript({
            name: api.pageParam.name,
            script: 'refresh()'
        });
    }

    //返回按钮
    function closeWin() {
        api.closeToWin({ name: api.pageParam.rootName });
        api.execScript({
            name: api.pageParam.rootName,
            script: 'refresh()'
        });
    }
</script>

</html>